<?php
/**
 * Created by PhpStorm.
 * User: xinghuo
 * Date: 2017/8/8
 * Time: 下午8:54
 */

namespace App\Http\Controllers;


use App\Models\IDName;
use App\Models\Test;
use BaiduBce\Services\Bos\BosOptions;
use Illuminate\Http\Request;
use Ramsey\Uuid\Uuid;
use ZeonWang\BaiduBosClient\BosInterface;

class ReportController
{
    use \App\TraitHelper\RequestHelper;

    function add(){
        return view('wechat');
    }
    public function url(Request $request){
        $jssdk = new \JSSDK(config('wechat.app_id'), config("wechat.secret"));
        $resUrl = $jssdk->downloadMedia($request->get('mid'));
//        $resUrl = 'https://api.weixin.qq.com/cgi-bin/media/get?access_token=LUGE7v6ClNnKPaXyveXr0M-A6-XxRPnlPzFtq0C7jUfVgFmHFe57j2CKOMyA2wVhy4gCOlHJdw42wtA4ZabvVFN8ZlbuYF9zp_3rZ5o1KXEFWSSl0q02pezR98xg__BVFDJcAJAOMS&media_id=K34105D5O7RBza4518eguWyUmry64ZLVTKCdRN_1JXX0gzJ_klcmhlJyNLkUL0lD';
        $bos = app(BosInterface::class);

        $client = new \GuzzleHttp\Client();

        $res = $client->request('GET', $resUrl);
        $http_response_header = $res->getHeader('Content-disposition');
        if (($http_response_header)){
            $http_response_header = $http_response_header[0];
        }
        $attachmentInfo = explode(' ',$http_response_header);
        $path = ['filename'];
        if ($attachmentInfo && $attachmentInfo[0] == 'attachment;') {
            parse_str($attachmentInfo[1],$path);
            if (isset($path['filename'])) {
                $path['filename'] = trim($path['filename'],'"');
            }
        }

        $uuid = strtoupper(Uuid::uuid4());

        $info = pathinfo($path['filename']);

        $fileName = '/tmp/' . $uuid;
        file_put_contents($fileName,  $res->getBody() );

        $user_meta = array("origin_name" => $path['filename']);
        $options = array(
            BosOptions::CONTENT_TYPE => mime_content_type($fileName),
            BosOptions::CONTENT_MD5 => base64_encode(hash_file("md5", $fileName, true)),
            BosOptions::CONTENT_LENGTH => filesize($fileName),
            BosOptions::CONTENT_SHA256 => hash_file("sha256", $fileName),
            BosOptions::USER_METADATA => $user_meta,
        );

        $objectKey = Date('/Y/m/d/H/') . $uuid . (isset($info['extension']) ? '.' . $info['extension'] : '');

        $d = $bos->client->putObjectFromFile(config('bos.bucket'), $objectKey, $fileName, $options);

        return $this->formatJson(isset($d->metadata),['file'=>$objectKey]);
    }
    public function store(Request $request){
       $wechat = $request->session()->get('wechat_user');
        if (isset($wechat['id'])) {
            $has = IDName::where('wx_ID',$wechat['id'])->first();
            if (!$has) {
                $we = new IDName();
                $we->wx_ID = $wechat['id'];
                $we->Name = $wechat['name'];
                $we->depart = '';
                $we->post = '';
                $we->userflag = 0;
                $we->save();
            }
            $test = new Test();
            $test->fromID = $wechat['id'];
            $test->content = $request->get('content');
            $test->msgtype = $request->get('type');
            $ims = $request->get('pics');
            isset( $ims[0]) && $test->img1_cl = $ims[0];
            isset( $ims[1]) && $test->img2_cl = $ims[1];
            isset( $ims[2]) && $test->img2_cl = $ims[3];
            $test->save();
        }
       var_dump($wechat['id']);
    }
}